<?php
class NddManageController extends ezcMvcController
{
	/**
	 * Create an empty Ndd
	 */
	public function doCreate()
	{
		$ret = new ezcMvcResult();

		$ndd = new NomDomaine();

		$ret->variables['ndd'] = $ndd;
		$ret->variables['recherche'] = '';

		return $ret;
	}

	/**
	 * Create and fill a Ndd base on posted data
	 * 
	 * @return ezcMvcResult Errors are set in an array called "errors"
	 */
	public function doCreateNdd()
	{
		$ret = new ezcMvcResult();
		$ret->variables['errors'] = array();

		//get db instance
		$psession = NddSession::getSession();

		//get data from post
		$date = '';
		$dateHebergement = '';

		//transform date
		try {
			$date = HelperDate::dateFrToMysql($_POST['expiration']);
		}
		catch( Exception $e) {
			$ret->variables['errors'][] = $e->getMessage();
		}

		if( trim($_POST['date_hebergement']) != '' ) {
			try {
				$dateHebergement = HelperDate::dateFrToMysql($_POST['date_hebergement']);
			}
			catch( Exception $e) {
				$ret->variables['errors'][] = $e->getMessage();
			}
		}
		
		//save to DB if there is no error
		if( count($ret->variables['errors']) == 0 ) {
			$ndd = new NomDomaine();
			$ndd->agresso			= $_POST['agresso'];
			$ndd->societe			= $_POST['societe'];
			$ndd->nomClient			= $_POST['client'];
			$ndd->domaine			= $_POST['domaine'];
			$ndd->expiration		= $date;
			$ndd->email				= $_POST['email'];
			$ndd->tel				= $_POST['tel'];
			$ndd->hebergement		= $_POST['hebergement'];
			$ndd->dateHebergement	= $dateHebergement;
			$ndd->facturation		= $_POST['facturation'];
			$ndd->commentaire		= $_POST['commentaire'];
	
			$psession->save( $ndd );
		}

		return $ret;
	}

	/**
	 * Edit a Ndd based on the ndd id given in the url
	 * 
	 * @return ezcMvcResult Errors are set in an array called "errors"
	 */
	public function doEdit()
	{
		$ret = new ezcMvcResult();

		$ret->variables['errors'] = array();

		//get db instance
		$psession = NddSession::getSession();

		if( !$this->nddId > 0) {
			$ret->variables['errors'][] = 'Bad ndd id';
			return $ret;
		}

		try {
			$ndd = $psession->load( 'NomDomaine', $this->nddId );
		}
		catch( Exception $e ) {
			$ret->variables['errors'][] = $e->getMessage();
		}

		$ret->variables['ndd'] = $ndd;
		$ret->variables['recherche'] = '';

		return $ret;
	}

	/**
	 * Update a Ndd based on the ndd id given in the POST variable
	 * 
	 * @return ezcMvcResult Errors are set in an array called "errors"
	 */
	public function doUpdateNdd()
	{
		$ret = new ezcMvcResult();
		$ret->variables['errors'] = array();

		//get db instance
		$psession = NddSession::getSession();

		//get data from post
		$date = '';
		$dateHebergement = '';

		//transform date
		try {
			$date = HelperDate::dateFrToMysql($_POST['expiration']);
		}
		catch( Exception $e) {
			$ret->variables['errors'][] = $e->getMessage();
		}

		if( trim($_POST['date_hebergement']) != '' ) {
			try {
				$dateHebergement = HelperDate::dateFrToMysql($_POST['date_hebergement']);
			}
			catch( Exception $e) {
				$ret->variables['errors'][] = $e->getMessage();
			}
		}

		//edit ndd
		if( !empty($_POST['nddId']) && $_POST['nddId'] > 0 ) {
			try {
				$ndd = $psession->load( 'NomDomaine', (int)$_POST['nddId'] );
			}
			catch( Exception $e ) {
				$ret->variables['errors'][] = $e->getMessage();
			}
		}
		else {
			$ret->variables['errors'][] = 'Valid Ndd id is required';
			return $ret;
		}

		if( count($ret->variables['errors']) == 0 ) {
			$ndd->agresso			= $_POST['agresso'];
			$ndd->societe			= $_POST['societe'];
			$ndd->nomClient			= $_POST['client'];
			$ndd->domaine			= $_POST['domaine'];
			$ndd->expiration		= $date;
			$ndd->email				= $_POST['email'];
			$ndd->tel				= $_POST['tel'];
			$ndd->hebergement		= $_POST['hebergement'];
			$ndd->dateHebergement	= $dateHebergement;
			$ndd->facturation		= $_POST['facturation'];
			$ndd->commentaire		= $_POST['commentaire'];
	
			$psession->update( $ndd );
		}

		return $ret;
	}
	
	/**
	 * Delete a Ndd based on the id given in the url
	 */
	public function doDelete() {
		$ret = new ezcMvcResult();
		$ret->variables['errors'] = array();

		//get db instance
		$psession = NddSession::getSession();
		
		if( !empty($_POST['nddId']) && $_POST['nddId'] > 0 ) {
			try {
				$ndd = $psession->load( 'NomDomaine', $_POST['nddId'] );
				$psession->delete($ndd);
			}
			catch( Exception $e ) {
				$ret->variables['errors'][] = $e->getMessage();
			}
		}
		else {
			$ret->variables['errors'][] = 'Valid Ndd id is required';
		}
		
		return $ret;
	}
	
	


}

?>